package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login01.do")
public class Login01Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rememberme = request.getParameter("rememberme");
        if ("admin".equals(username) && "123".equals(password)) {
            Cookie cookie01 = new Cookie("username", username);
            Cookie cookie02 = new Cookie("password", password);
            cookie01.setMaxAge(30 * 24 * 60 * 60);
            cookie02.setMaxAge(30 * 24 * 60 * 60);
            response.addCookie(cookie01);
            response.addCookie(cookie02);
            Cookie cookie03 = new Cookie("rememberme", rememberme);
            if (rememberme != null) {
                //勾选记住我
                cookie03.setMaxAge(30 * 24 * 60 * 60);
            } else {
                //没有勾选记住我
                //删除 rememberme 的cookie
                cookie03.setMaxAge(0);
            }
            response.addCookie(cookie03);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
